package com.googlecode.moneyManager.model;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.Transient;

/**
 * 
 * @author Sergiy Doroshenko
 * 
 */
@Entity
public class Percentage extends BaseEntity {
    final static BigDecimal HUNDRED = new BigDecimal("100");

    public static Percentage ZERO = new Percentage("0");

    private BigDecimal value;
    
    public Percentage(){}

    /**
         * 
         * @param value
         */
    public Percentage(String value) {
	this(new BigDecimal(value).setScale(2));
    }

    /**
         * 
         * @param value
         */
    public Percentage(BigDecimal value) {
	if (value == null)
	    throw new IllegalArgumentException("value must not be null");
	if (BigDecimal.ZERO.compareTo(value) == 1
		|| HUNDRED.compareTo(value) == -1)
	    throw new IllegalArgumentException(
		    "percentage values must be in rage 0 <= value <= 100");
	this.value = value;
    }

    /**
         * get number represent percent devide value on 100
         * 
         * @return
         */
    @Transient
    public BigDecimal getNumberOfPercent() {
	return value.divide(HUNDRED);
    }

    /**
         * get percent from 0 to 100
         * 
         * @return
         */
    public BigDecimal getValue() {
	return value;
    }

    public void setValue(BigDecimal value) {
	this.value = value;
    }

}
